#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <gui.h>
#include <uview.h>
#include <sys/proc.h>
#include <termios.h>

#include "terminal.h"

void terminal_main()
{
    terminal_window_loop();
}

void terminal_quit()
{
    terminal_console_exit();
    terminal_window_exit();
}

int terminal_exec(void *arg)
{
    pid_t pid;
    int status;

    pid = fork();
    if (pid < 0)
    {
        dprintf("\nterminal fork failed!\n");
        return -1;
    }
    else
    {
        if (!pid) // children
        {
            terminal_ptty_launch(&pty,RAW_SHELL_PATH);
        }
        else // father
        {
            terminal_ptty_setup(pid);
        }
    }
}

int main(int argc, char **argv)
{
    if (terminal_console_init() < 0)
        return -1;

    if(terminal_cursor_init()<0)
        return -1;

    if (terminal_ptty_init() < 0)
        return -1;
    
    if (terminal_exec(NULL) < 0)
        return -1;

    terminal_main();
    terminal_quit();
}
